package jgame.model;
import jgame.Log;
import jgame.model.movingTypes.*;

public class Movable extends Unit {
	float mass;
	boolean isMoving = false;
	Position movingTarget;
	
	MovingType defaultMovingType;
	MovingType currentMovingType;
	
	public MovingType getDefaultMovingType() {
		return defaultMovingType;
	}

	public void setDefaultMovingType(MovingType defaultMovingType) {
		this.defaultMovingType = defaultMovingType;
	}
	
	public MovingType getCurrentMovingType() {
		return currentMovingType == null ? defaultMovingType : currentMovingType;
	}

	public void setCurrentMovingType(MovingType currentMovingType) {
		this.currentMovingType = currentMovingType;
	}

	public void moveTo(Unit target) {
		moveTo(target.getPosition());
	}

	public void moveTo(Position target) {
		try {
			if(this.getCurrentMovingType() == null) {
				throw new Exception("MovingType not set");
			}
		//	Log.add("I'm "+this.getCurrentMovingType()+" to " + target.getX() + ":" + target.getY());
			this.setMovingTarget(target);
			this.isMoving = true;
		}
		catch(Exception e) {
			Log.addException(e.getMessage());
		}
		
		
	}

	public void setMovingTarget(Position target) {
		this.movingTarget = target;
	}

	public Position getMovingTarget() {
		return this.movingTarget;
	}

	public void moveTick() {
		if(this.getMovingTarget() != null && this.isMoving == true) {
			Position myPosition = this.getPosition();
			
			if(this.getMovingTarget().getX() > myPosition.getX()) {
				myPosition.setX((int)((float)myPosition.getX() + this.getCurrentMovingType().getFactor()));
			} else if(this.getMovingTarget().getX() < myPosition.getX()) {
				myPosition.setX((int)((float)myPosition.getX() - this.getCurrentMovingType().getFactor()));
			}
			
			if(this.getMovingTarget().getY() > myPosition.getY()) {
				myPosition.setY((int)((float)myPosition.getY() + this.getCurrentMovingType().getFactor()));
			} else if(this.getMovingTarget().getY() < myPosition.getY()) {
				myPosition.setY((int)((float)myPosition.getY() - this.getCurrentMovingType().getFactor()));
			}
			
			this.setPosition(myPosition);

			if(myPosition.equals(this.getMovingTarget())) {
				this.isMoving = false;
				this.setMovingTarget(null);
			}
		}
		
		super.moveTick();
	}

}
